<!-- HTML header for doxygen 1.8.13-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Taskflow Handbook</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="https://taskflow.github.io/">Taskflow</a>
   &#160;<span id="projectnumber">3.0.0-Master-Branch</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('cuda__memory_8hpp_source.html','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">cuda_memory.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="cuda__memory_8hpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#pragma once</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="cuda__device_8hpp.html">cuda_device.hpp</a>&quot;</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespacetf.html">tf</a> {</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment">// ----------------------------------------------------------------------------</span></div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment">// memory</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment">// ----------------------------------------------------------------------------</span></div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;</div><div class="line"><a name="l00018"></a><span class="lineno"><a class="line" href="cuda__memory_8hpp.html#a1effcf929b7e488925f9e12d74c8c62b">   18</a></span>&#160;<span class="keyword">inline</span> <span class="keywordtype">size_t</span> <a class="code" href="cuda__memory_8hpp.html#a1effcf929b7e488925f9e12d74c8c62b">cuda_get_free_mem</a>(<span class="keywordtype">int</span> d) {</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;  <a class="code" href="classtf_1_1cudaScopedDevice.html">cudaScopedDevice</a> ctx(d);</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;  <span class="keywordtype">size_t</span> free, total;</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;  TF_CHECK_CUDA(</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;    cudaMemGetInfo(&amp;free, &amp;total), <span class="stringliteral">&quot;failed to get mem info on device &quot;</span>, d</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;  );</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;  <span class="keywordflow">return</span> free;</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;}</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;</div><div class="line"><a name="l00030"></a><span class="lineno"><a class="line" href="cuda__memory_8hpp.html#a58bbc8d5d955582d6b5f7fdac51d010b">   30</a></span>&#160;<span class="keyword">inline</span> <span class="keywordtype">size_t</span> <a class="code" href="cuda__memory_8hpp.html#a58bbc8d5d955582d6b5f7fdac51d010b">cuda_get_total_mem</a>(<span class="keywordtype">int</span> d) {</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;  <a class="code" href="classtf_1_1cudaScopedDevice.html">cudaScopedDevice</a> ctx(d);</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;  <span class="keywordtype">size_t</span> free, total;</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;  TF_CHECK_CUDA(</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;    cudaMemGetInfo(&amp;free, &amp;total), <span class="stringliteral">&quot;failed to get mem info on device &quot;</span>, d</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;  );</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;  <span class="keywordflow">return</span> total;</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;}</div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;<span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00047"></a><span class="lineno"><a class="line" href="cuda__memory_8hpp.html#a6f04fd3168c45eeb2dffb223e5c81e45">   47</a></span>&#160;T* <a class="code" href="cuda__memory_8hpp.html#a6f04fd3168c45eeb2dffb223e5c81e45">cuda_malloc_device</a>(<span class="keywordtype">size_t</span> N, <span class="keywordtype">int</span> d) {</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;  <a class="code" href="classtf_1_1cudaScopedDevice.html">cudaScopedDevice</a> ctx(d);</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;  T* ptr {<span class="keyword">nullptr</span>};</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;  TF_CHECK_CUDA(</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;    cudaMalloc(&amp;ptr, N*<span class="keyword">sizeof</span>(T)), </div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;    <span class="stringliteral">&quot;failed to allocate memory (&quot;</span>, N*<span class="keyword">sizeof</span>(T), <span class="stringliteral">&quot;bytes) on device &quot;</span>, d</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;  )</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;  <span class="keywordflow">return</span> ptr;</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;}</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;<span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00064"></a><span class="lineno"><a class="line" href="cuda__memory_8hpp.html#ab9b68b8f4336f13b190d573969cb1cf7">   64</a></span>&#160;T* <a class="code" href="cuda__memory_8hpp.html#a6f04fd3168c45eeb2dffb223e5c81e45">cuda_malloc_device</a>(<span class="keywordtype">size_t</span> N) {</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;  <span class="keywordflow">return</span> cuda_malloc_device&lt;T&gt;(N, <a class="code" href="cuda__device_8hpp.html#a235f5a9ce203d538eec1f4114221d473">cuda_get_device</a>());</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;}</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;</div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;<span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00075"></a><span class="lineno"><a class="line" href="cuda__memory_8hpp.html#a8eed05685b030fc44703213a4ef86f11">   75</a></span>&#160;T* <a class="code" href="cuda__memory_8hpp.html#a8eed05685b030fc44703213a4ef86f11">cuda_malloc_shared</a>(<span class="keywordtype">size_t</span> N) {</div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;  T* ptr {<span class="keyword">nullptr</span>};</div><div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;  TF_CHECK_CUDA(</div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;    cudaMallocManaged(&amp;ptr, N*<span class="keyword">sizeof</span>(T)), </div><div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;    <span class="stringliteral">&quot;failed to allocate shared memory (&quot;</span>, N*<span class="keyword">sizeof</span>(T), <span class="stringliteral">&quot;bytes)&quot;</span></div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;  )</div><div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;  <span class="keywordflow">return</span> ptr;</div><div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;}</div><div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;</div><div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;<span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00095"></a><span class="lineno"><a class="line" href="cuda__memory_8hpp.html#ac7a8fe7456b888d6072ba94783c5003c">   95</a></span>&#160;<span class="keywordtype">void</span> <a class="code" href="cuda__memory_8hpp.html#ac7a8fe7456b888d6072ba94783c5003c">cuda_free</a>(T* ptr, <span class="keywordtype">int</span> d) {</div><div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;  <a class="code" href="classtf_1_1cudaScopedDevice.html">cudaScopedDevice</a> ctx(d);</div><div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;  TF_CHECK_CUDA(cudaFree(ptr), <span class="stringliteral">&quot;failed to free memory &quot;</span>, ptr);</div><div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;}</div><div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;</div><div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;<span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00110"></a><span class="lineno"><a class="line" href="cuda__memory_8hpp.html#ae174a3a49b91ef21554dac16806f0d72">  110</a></span>&#160;<span class="keywordtype">void</span> <a class="code" href="cuda__memory_8hpp.html#ac7a8fe7456b888d6072ba94783c5003c">cuda_free</a>(T* ptr) {</div><div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;  <a class="code" href="cuda__memory_8hpp.html#ac7a8fe7456b888d6072ba94783c5003c">cuda_free</a>(ptr, <a class="code" href="cuda__device_8hpp.html#a235f5a9ce203d538eec1f4114221d473">cuda_get_device</a>());</div><div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;}</div><div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;</div><div class="line"><a name="l00126"></a><span class="lineno"><a class="line" href="cuda__memory_8hpp.html#aa4266474b921f8ed7d9ec8071fded2a4">  126</a></span>&#160;<span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="cuda__memory_8hpp.html#aa4266474b921f8ed7d9ec8071fded2a4">cuda_memcpy_async</a>(</div><div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;  cudaStream_t stream, <span class="keywordtype">void</span>* dst, <span class="keyword">const</span> <span class="keywordtype">void</span>* src, <span class="keywordtype">size_t</span> count</div><div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;) {</div><div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;  TF_CHECK_CUDA(</div><div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;    cudaMemcpyAsync(dst, src, count, cudaMemcpyDefault, stream),</div><div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;    <span class="stringliteral">&quot;failed to perform cudaMemcpyAsync&quot;</span></div><div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;  );</div><div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;}</div><div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;</div><div class="line"><a name="l00147"></a><span class="lineno"><a class="line" href="cuda__memory_8hpp.html#a6615554d2954e895755411ee444d9760">  147</a></span>&#160;<span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="cuda__memory_8hpp.html#a6615554d2954e895755411ee444d9760">cuda_memset_async</a>(</div><div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;  cudaStream_t stream, <span class="keywordtype">void</span>* devPtr, <span class="keywordtype">int</span> value, <span class="keywordtype">size_t</span> count</div><div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;){</div><div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;  TF_CHECK_CUDA(</div><div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;    cudaMemsetAsync(devPtr, value, count, stream),</div><div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;    <span class="stringliteral">&quot;failed to perform cudaMemsetAsync&quot;</span></div><div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;  );</div><div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;}</div><div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;</div><div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160;}  <span class="comment">// end of namespace tf -----------------------------------------------------</span></div><div class="line"><a name="l00157"></a><span class="lineno">  157</span>&#160;</div><div class="line"><a name="l00158"></a><span class="lineno">  158</span>&#160;</div><div class="line"><a name="l00159"></a><span class="lineno">  159</span>&#160;</div><div class="line"><a name="l00160"></a><span class="lineno">  160</span>&#160;</div><div class="line"><a name="l00161"></a><span class="lineno">  161</span>&#160;</div><div class="line"><a name="l00162"></a><span class="lineno">  162</span>&#160;</div><div class="ttc" id="cuda__memory_8hpp_html_aa4266474b921f8ed7d9ec8071fded2a4"><div class="ttname"><a href="cuda__memory_8hpp.html#aa4266474b921f8ed7d9ec8071fded2a4">tf::cuda_memcpy_async</a></div><div class="ttdeci">void cuda_memcpy_async(cudaStream_t stream, void *dst, const void *src, size_t count)</div><div class="ttdoc">copies data between host and device asynchronously through a stream </div><div class="ttdef"><b>Definition:</b> cuda_memory.hpp:126</div></div>
<div class="ttc" id="cuda__memory_8hpp_html_ac7a8fe7456b888d6072ba94783c5003c"><div class="ttname"><a href="cuda__memory_8hpp.html#ac7a8fe7456b888d6072ba94783c5003c">tf::cuda_free</a></div><div class="ttdeci">void cuda_free(T *ptr, int d)</div><div class="ttdoc">frees memory on the GPU device </div><div class="ttdef"><b>Definition:</b> cuda_memory.hpp:95</div></div>
<div class="ttc" id="classtf_1_1cudaScopedDevice_html"><div class="ttname"><a href="classtf_1_1cudaScopedDevice.html">tf::cudaScopedDevice</a></div><div class="ttdoc">RAII-styled device context switch. </div><div class="ttdef"><b>Definition:</b> cuda_device.hpp:292</div></div>
<div class="ttc" id="cuda__memory_8hpp_html_a1effcf929b7e488925f9e12d74c8c62b"><div class="ttname"><a href="cuda__memory_8hpp.html#a1effcf929b7e488925f9e12d74c8c62b">tf::cuda_get_free_mem</a></div><div class="ttdeci">size_t cuda_get_free_mem(int d)</div><div class="ttdoc">queries the free memory (expensive call) </div><div class="ttdef"><b>Definition:</b> cuda_memory.hpp:18</div></div>
<div class="ttc" id="namespacetf_html"><div class="ttname"><a href="namespacetf.html">tf</a></div><div class="ttdef"><b>Definition:</b> flow_builder.hpp:9</div></div>
<div class="ttc" id="cuda__memory_8hpp_html_a8eed05685b030fc44703213a4ef86f11"><div class="ttname"><a href="cuda__memory_8hpp.html#a8eed05685b030fc44703213a4ef86f11">tf::cuda_malloc_shared</a></div><div class="ttdeci">T * cuda_malloc_shared(size_t N)</div><div class="ttdoc">allocates shared memory for holding N elements of type T </div><div class="ttdef"><b>Definition:</b> cuda_memory.hpp:75</div></div>
<div class="ttc" id="cuda__memory_8hpp_html_a58bbc8d5d955582d6b5f7fdac51d010b"><div class="ttname"><a href="cuda__memory_8hpp.html#a58bbc8d5d955582d6b5f7fdac51d010b">tf::cuda_get_total_mem</a></div><div class="ttdeci">size_t cuda_get_total_mem(int d)</div><div class="ttdoc">queries the total available memory (expensive call) </div><div class="ttdef"><b>Definition:</b> cuda_memory.hpp:30</div></div>
<div class="ttc" id="cuda__device_8hpp_html"><div class="ttname"><a href="cuda__device_8hpp.html">cuda_device.hpp</a></div></div>
<div class="ttc" id="cuda__device_8hpp_html_a235f5a9ce203d538eec1f4114221d473"><div class="ttname"><a href="cuda__device_8hpp.html#a235f5a9ce203d538eec1f4114221d473">tf::cuda_get_device</a></div><div class="ttdeci">int cuda_get_device()</div><div class="ttdoc">gets the current device associated with the caller thread </div><div class="ttdef"><b>Definition:</b> cuda_device.hpp:23</div></div>
<div class="ttc" id="cuda__memory_8hpp_html_a6f04fd3168c45eeb2dffb223e5c81e45"><div class="ttname"><a href="cuda__memory_8hpp.html#a6f04fd3168c45eeb2dffb223e5c81e45">tf::cuda_malloc_device</a></div><div class="ttdeci">T * cuda_malloc_device(size_t N, int d)</div><div class="ttdoc">allocates memory on the given device for holding N elements of type T </div><div class="ttdef"><b>Definition:</b> cuda_memory.hpp:47</div></div>
<div class="ttc" id="cuda__memory_8hpp_html_a6615554d2954e895755411ee444d9760"><div class="ttname"><a href="cuda__memory_8hpp.html#a6615554d2954e895755411ee444d9760">tf::cuda_memset_async</a></div><div class="ttdeci">void cuda_memset_async(cudaStream_t stream, void *devPtr, int value, size_t count)</div><div class="ttdoc">initializes or sets GPU memory to the given value byte by byte </div><div class="ttdef"><b>Definition:</b> cuda_memory.hpp:147</div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_88dad41ea55ca2177e141d32a93e931c.html">taskflow</a></li><li class="navelem"><a class="el" href="dir_638d51f8e6f20ea8c720cc8c006296ba.html">cuda</a></li><li class="navelem"><a class="el" href="cuda__memory_8hpp.html">cuda_memory.hpp</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
  </ul>
</div>
</body>
</html>
